package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisDemoZSet {
    public static void test1(Jedis jedis) {
        System.out.println("zadd 和 zrange");
        jedis.flushAll();
        jedis.zadd("key",1.0,"zhangsan");
        Map<String ,Double> map=new HashMap<>();
        map.put("lisi",2.0);
        map.put("wangwu",3.0);
        map.put("zhaoliu",4.0);
        map.put("sunqi",5.0);
        jedis.zadd("key",map);
        List<String> key = jedis.zrange("key", 0, -1);
        System.out.println("key : "+ key);
        List<Tuple> key1 = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("key1 : "+ key1);
        String element = key1.get(1).getElement();
        System.out.println("element : "+ element);
        double score = key1.get(1).getScore();
        System.out.println("score : "+ score);
    }
    private static void test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();
        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");
        long key = jedis.zcard("key");
        System.out.println("key : " + key);
    }
    private static void test3(Jedis jedis) {
        System.out.println("zrem");
        jedis.flushAll();
        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");
        long zrem = jedis.zrem("key", "lisi", "zhangsan");
        System.out.println("zrem : " + zrem);
        List<Tuple> key = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("key : " + key);
    }
    public static void test4(Jedis jedis) {
        System.out.println("zscore");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");
        Double zscore = jedis.zscore("key", "aaa");
        System.out.println("zscore : " + zscore);
    }
    public static void test5(Jedis jedis) {
        System.out.println("zrank");
        jedis.flushAll();

        jedis.zadd("key", 10, "zhangsan");
        jedis.zadd("key", 20, "lisi");
        jedis.zadd("key", 30, "wangwu");
        Long zrank = jedis.zrank("key", "aaa");
        System.out.println("zrank : " + zrank);

    }
    public static void main(String[] args) {
        JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis=jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
